#ifndef __UART_UTILS_H
#define __UART_UTILS_H


#include "main.h"



/**
 * @brief Fill mode definitions for binary output
 */
#define NOFILLED	0	/**< No padding with leading zeros */
#define FILLED		1	/**< Pad with leading zeros to 8 bits */


/**
 * @brief UART device structure
 */
struct uart_device{
    char *name;
    void (*init)(struct uart_device *pDev, int baud, int data_bits, int stop_bits, int parity);
    void (*send)(struct uart_device *pDev, unsigned char *data, int length);
    char (*recv)(struct uart_device *pDev, unsigned char *data);
    void *priv_data;
};

struct priv_struct{
    UART_HandleTypeDef *pUart;
};

/**
 * find uart device
 * 
 * @param name the name of the uart device
 * 
 * @return the uart device
 */
struct uart_device * find_uart_device(char *name);

/**
 * put char to uart
 * 
 * @param data the char to be put
 * 
 * @return void
 */
void myputchar(char data);

/**
 * get char from uart
 * 
 * @param void
 * 
 * @return the char got
 */
char mygetchar(void);

/**
 * put string to uart
 * 
 * @param str the string to be put
 * 
 * @return void
 */
void myputstr(const char *str);


/**
 * @brief Convert decimal number to hex (not implemented)
 * 
 * @param dnum the decimal number to be converted
 * 
 * @return void
 */
void dec2hex(unsigned int dnum);

/**
 * put hex to uart
 * 
 * @param hex the hex to be put
 * 
 * @return void
 */
/**
 * @brief Put 8-bit hex value to uart
 * 
 * @param val the 8-bit value to be converted and output as hex
 * 
 * @return void
 */
void myputhex8(unsigned char val);

/**
 * @brief Put 16-bit hex value to uart
 * 
 * @param val the 16-bit value to be converted and output as hex
 * 
 * @return void
 */
void myputhex16(unsigned short val);

/**
 * @brief Put 32-bit hex value to uart
 * 
 * @param val the 32-bit value to be converted and output as hex
 * 
 * @return void
 */
void myputhex32(unsigned int val);

/**
 * @brief Put decimal value to uart
 * 
 * @param val the decimal value to be converted and output as decimal
 * 
 * @return void
 */
void myputdec(unsigned int val);

/**
 * @brief Put float value to uart
 * 
 * @param val the float value to be converted and output as decimal
 * 
 * @return void
 */
void myputfloat(float val);

/**
 * @brief Put binary value to uart
 * 
 * @param val the binary value to be converted and output as binary
 * @param fill_mode if FILLED, pad with leading zeros to 8 bits; if NOFILLED, output without padding
 * 
 * @return void
 */
void myputbin(unsigned int val, unsigned char fill_mode);

#endif

